package com.ds.infrastructure.hyperspace.console.model.request.containerconfig.configdata;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.ds.infrastructure.hyperspace.console.dto.configdata.PipelinesDTO;
import com.ds.infrastructure.hyperspace.console.dto.configdata.PreflowDTO;
import com.ds.infrastructure.hyperspace.console.utils.ValidationUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-09-20 16:40
 **/
@Data
@NoArgsConstructor
public class PipelinesForm {
    @NotNull
    private String id;

    @JsonProperty("preFlow")
    private List<PreflowForm> preflow;
    @JsonProperty("postFlow")
    private List<PreflowForm> postflow;

    public static PipelinesDTO toDTO(PipelinesForm pipelines, String entryEndpointNamespace, String targetEndpointNamespace) {
        PipelinesDTO pipelinesDTO = new PipelinesDTO();
        //
        pipelinesDTO.setId(pipelines.getId());
        //
        pipelinesDTO.setEntryendpoint(entryEndpointNamespace);
        addProFlowDTO(pipelines, pipelinesDTO);
        //
        pipelinesDTO.setTargetendpoint(targetEndpointNamespace);
        addPostFlowDTO(pipelines, pipelinesDTO);
        return pipelinesDTO;
    }

    private static void addPostFlowDTO(PipelinesForm pipelines, PipelinesDTO pipelinesDTO) {
        if (pipelines.getPostflow() == null || pipelines.getPostflow().isEmpty()) {
            return;
        }
        ValidationUtil.validateEntityList(pipelines.getPostflow(), "postFlow");
        List<PreflowDTO> postflowDTOList = new ArrayList<>();
        pipelines.getPostflow().forEach(item -> postflowDTOList.add(PreflowForm.toDTO(item)));
        pipelinesDTO.setPostflow(postflowDTOList);
    }

    private static void addProFlowDTO(PipelinesForm pipelines, PipelinesDTO pipelinesDTO) {
        if (pipelines.getPreflow() == null || pipelines.getPreflow().isEmpty()) {
            return;
        }
        ValidationUtil.validateEntityList(pipelines.getPreflow(), "preFlow");
        List<PreflowDTO> preFlowFormList = new ArrayList<>();
        pipelines.getPreflow().forEach(item -> preFlowFormList.add(PreflowForm.toDTO(item)));
        pipelinesDTO.setPreflow(preFlowFormList);
    }

    public static PipelinesForm toVO(PipelinesDTO dto) {
        PipelinesForm pipelinesFrom = new PipelinesForm();
        pipelinesFrom.setId(dto.getId());
        addPreFlowVO(dto, pipelinesFrom);
        addPostFlowVO(dto, pipelinesFrom);
        return pipelinesFrom;
    }

    private static void addPostFlowVO(PipelinesDTO dto, PipelinesForm pipelinesFrom) {
        if (dto.getPostflow() == null || dto.getPostflow().isEmpty()) {
            return;
        }
        List<PreflowForm> postflowFromList = new ArrayList<>();
        dto.getPostflow().forEach(item -> postflowFromList.add(PreflowForm.toVO(item)));
        pipelinesFrom.setPostflow(postflowFromList);
    }

    private static void addPreFlowVO(PipelinesDTO dto, PipelinesForm pipelinesFrom) {
        if (dto.getPreflow() == null || dto.getPreflow().isEmpty()) {
            return;
        }
        List<PreflowForm> preflowFromList = new ArrayList<>();
        dto.getPreflow().forEach(item -> preflowFromList.add(PreflowForm.toVO(item)));
        pipelinesFrom.setPreflow(preflowFromList);
    }
}
